﻿using System.Collections.Generic;

namespace VirtualTournaments.MatchProvider
{
	public class ShowableMatch
	{
		private string matchDate;
		private long matchHalfCicles;
		public int participantSize;
		public int ballSize;
		private ShowableParticipant matchBall;
		private List<ShowableTeam> teamsInMatch;
		private List<ShowableParticipant> participantsInMatch;
		private List<ShowableEvent> eventsInMatch;

		public long MatchHalfCicles
		{
			get { return matchHalfCicles; }
			set { matchHalfCicles = value; }
		}

		public ShowableMatch(string xMatchDate, long xMatchCicles, int xBallSize, int xParticipantSize)
		{
			matchDate = xMatchDate;
			matchHalfCicles = xMatchCicles;
			ballSize = xBallSize;
			participantSize = xParticipantSize;
			teamsInMatch = new List<ShowableTeam>();
			participantsInMatch = new List<ShowableParticipant>();
			eventsInMatch = new List<ShowableEvent>();
		}

		public void loadUpParticipants()
		{
			foreach (ShowableTeam team in teamsInMatch)
			{
				foreach (ShowableParticipant participant in team.getParticipants())
					participantsInMatch.Add(participant);
			}
		}

		public void addEvent(ShowableEvent xEvent)
		{
			eventsInMatch.Add(xEvent);
		}

		public List<ShowableEvent> eventsOnCicle(long xCicle)
		{
			List<ShowableEvent> eventList = new List<ShowableEvent>();
			foreach (ShowableEvent matchEvent in this.eventsInMatch)
			{
				if (matchEvent.cicle == xCicle)
					eventList.Add(matchEvent);
			}
			return eventList;
		}

		public string getTeamName(string p)
		{
			if (p.Equals("A"))
				return this.teamsInMatch[0].teamName;
			else
				return this.teamsInMatch[1].teamName;
		}

		public string getParticipantName(string p, int p_2)
		{
			if (p.Equals("A"))
				return this.teamsInMatch[0].getParticipantName(p_2);
			else
				return this.teamsInMatch[1].getParticipantName(p_2);
		}

		public ShowableParticipant getParticipant(string p, int p_2)
		{
			if (p.Equals("A"))
				return this.teamsInMatch[0].getParticipant(p_2);
			else
				return this.teamsInMatch[1].getParticipant(p_2);
		}

		internal void addTeam(ShowableTeam xTeam)
		{
			this.teamsInMatch.Add(xTeam);
		}

		// TODO: be aware of errors!
		internal ShowableTeam getTeamByID(string xTeamID)
		{
			foreach (ShowableTeam team in teamsInMatch)
			{
				if (team.teamID == xTeamID)
					return team;
			}
			return null;
		}


		public void setMatchBall(ShowableParticipant xBall)
		{
			this.matchBall = xBall;
		}

		// TODO: be aware of errors!
		public ShowableParticipant getMatchBall()
		{
			return this.matchBall;
		}

		public List<ShowableTeam> getTeamsInMatch()
		{
			return teamsInMatch;
		}
	}
}
